{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Single-Qubit System Measurements Tutorial Workbook\n",
    "\n",
    "**What is this workbook?**\n",
    "A workbook is a collection of problems, accompanied by solutions to them. \n",
    "The explanations focus on the logical steps required to solve a problem; they illustrate the concepts that need to be applied to come up with a solution to the problem, explaining the mathematical steps required. \n",
    "\n",
    "Note that a workbook should not be the primary source of knowledge on the subject matter; it assumes that you've already read a tutorial or a textbook and that you are now seeking to improve your problem-solving skills. You should attempt solving the tasks of the respective kata first, and turn to the workbook only if stuck. While a textbook emphasizes knowledge acquisition, a workbook emphasizes skill acquisition.\n",
    "\n",
    "This workbook describes the solutions to the problems offered in the [Single-Qubit System Measurements tutorial](./SingleQubitSystemMeasurements.ipynb). \n",
    "Since the tasks are offered as programming problems, the explanations also cover some elements of Q# that might be non-obvious for a first-time user.\n",
    "\n",
    "**What you should know for this workbook**\n",
    "\n",
    "You should be familiar with the following concepts before tackling the Single-Qubit System Measurements tutorial (and this workbook):\n",
    "1. Basic linear algebra\n",
    "2. The concept of a qubit\n",
    "3. Single-qubit gates\n",
    "$\\renewcommand{\\ket}[1]{\\left\\lvert#1\\right\\rangle}$\n",
    "$\\renewcommand{\\bra}[1]{\\left\\langle#1\\right\\rvert}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 1</span>: The probability outcomes for a specific state\n",
    "The qubit is in the following state:\n",
    "$$\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1 \\equiv \\begin{bmatrix} 0.6 \\\\ 0.8\\end{bmatrix}.$$\n",
    "\n",
    "If this qubit is measured in the computational basis, what are the outcome probabilities?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "The given state $\\ket \\psi$ is normalized, since $0.6^2 + 0.8^2 = 1$. Hence, the probability of measuring $0$ is $|0.6|^2 = 0.36$, and the probability of measuring $1$ is $|0.8|^2 = 0.64$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 1 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-1:-The-probability-outcomes-for-a-specific-state)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 2</span>: Distinguish $|0\\rangle$ and $|1\\rangle$\n",
    "\n",
    "**Input:** A qubit which is guaranteed to be in either the $|0\\rangle$ or the $|1\\rangle$ state.\n",
    "\n",
    "**Output:**  `true` if the qubit was in the $|0\\rangle$ state, or `false` if it was in the $|1\\rangle$ state. The state of the qubit at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "The input qubit is guaranteed to be either in basis state $|0\\rangle$ or $|1\\rangle$. This means that when measuring the qubit in the computational basis, the measurement will report the input state without any doubt.  \n",
    "\n",
    "In Q# the operation [`M`](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.m) can be used to measure a single qubit in the computational basis. The measurement result is a value of type `Result`: the operation `M` will return `One` if the input qubit was in the $|1\\rangle$ state and `Zero` if the input qubit was in the $|0\\rangle$ state. Since we need to encode the first case as `false` and the second one as `true`, we can return the result of equality comparison between measurement result and `Zero`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T2_IsQubitZero\n",
    "\n",
    "operation IsQubitZero (q : Qubit) : Bool {\n",
    "    return M(q) == Zero;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 2 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-2:-Distinguish-$|0\\rangle$-and-$|1\\rangle$)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 3</span>: Distinguish $|+\\rangle$ and $|-\\rangle$ using the `Measure` operation\n",
    "\n",
    "**Input**: A qubit which is guaranteed to be in either the $\\ket +$ state, or the $\\ket -$ state.\n",
    "\n",
    "**Output**: `true` if the qubit is in the $\\ket +$ state, or `false` if it was in the $\\ket -$ state."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "The input qubit is guaranteed to be either in basis state $|+\\rangle$ or $|-\\rangle$. This means that when measuring the qubit in the Pauli $X$ basis, the measurement will report the input state without any doubt. (Recall that these states are eigenstates for the Pauli $X$ matrix).  \n",
    "\n",
    "In Q# the operation [`Measure`](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.measure) can be used to measure a qubit in Pauli basis of the user's choice. The operation returns a value of type `Result`, and is `Zero` if the measured state corresponds to the eigenvalue $+1$, and `One` if it corresponds to the eigenvalue $-1$ of the Pauli operator. \n",
    "\n",
    "Since the states $\\ket +$ and $\\ket -$ correspond to the eigenvalues $+1$ and $-1$ of the Pauli X operator, we can return the result of equality comparison between the measurement result and `One`. \n",
    "Note that since `Measure` operation generally works with multiple qubits to perform multi-qubit measurements, it takes array parameters. To do a single-qubit measurement, you need to pass two arrays of one element, `[PauliX]` and `[q]`, rather than individual values."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T3_IsQubitMinus\n",
    "\n",
    "operation IsQubitMinus (q : Qubit) : Bool {\n",
    "    return Measure([PauliX], [q]) == One;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 3 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-3:-Distinguish-$|+\\rangle$-and-$|-\\rangle$-using-the-Measure-operation)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 4</span>: The outcome probabilities for a measurement in a specified basis\n",
    "<ol>\n",
    "<li>What are the outcome probabilities of measuring a qubit in the $\\ket{0}$ state in the Pauli X basis, i.e., the $\\{ \\ket +, \\ket -\\}$ basis?</li>\n",
    "<li>What are the outcome probabilities of measuring a qubit in the $0.6\\ket{0} + 0.8 \\ket{1}$ state in the Pauli Y basis, i.e., the $\\{ \\ket i, \\ket{-i}\\}$ basis?</li>\n",
    "</ol> "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "\n",
    "1. To find the probabilities of measuring $+$ and $-$, we first need to express the state $\\ket 0$ in terms of $\\ket +$ and $\\ket -$. Using the fact that $\\ket{\\pm} = \\frac{1}{\\sqrt{2}}  (\\ket{0} \\pm \\ket{1})$, we can show that \n",
    "$$\n",
    "\\ket 0 = \\frac{1}{\\sqrt{2}} \\ket{+} + \\frac{1}{\\sqrt{2}} \\ket{-}.\n",
    "$$\n",
    "  \n",
    "  Thus, the probability of measuring $+$ is $|\\frac1{\\sqrt2}|^2 = 0.5$, and similarly, the probability of measuring $-$ is $0.5$.\n",
    "\n",
    "2. Similar to the first part, we need to express the state $\\ket \\psi = 0.6 \\ket 0 + 0.8 \\ket 1$ in the $\\ket{\\pm i}$ basis. For this calculation, we use the projection matrix approach.\n",
    "First, we recall that the states $\\ket{\\pm i}$ are given by\n",
    "$$\n",
    "\\ket{\\pm i} = \\frac1{\\sqrt2} (\\ket 0 \\pm i \\ket 1).\n",
    "$$\n",
    "We can now construct the two projectors $P_{\\pm i}$ onto states $\\ket {\\pm i}$ as follows:\n",
    "\\begin{align}\n",
    "P_{i} &= \\ket{i}\\bra{i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\ i \\end{bmatrix} \\begin{bmatrix} 1 & -i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\ i & 1\\end{bmatrix}; \\\\\n",
    "P_{-i} &=\\ket{-i}\\bra{-i} = \\frac{1}{2} \\begin{bmatrix} 1 \\\\ -i \\end{bmatrix} \\begin{bmatrix} 1 & i \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix}1 & i \\\\ -i & 1\\end{bmatrix}\n",
    "\\end{align}\n",
    "Recalling that the probabilities of measuring $\\pm i$ are equal to the norm of the vectors $P_{\\pm i}\\ket \\psi$, we now apply $P_{\\pm i}$ to $\\ket \\psi$:\n",
    "\\begin{align}\n",
    "P_{+i} \\ket \\psi &= \\frac{1}{2} \\begin{bmatrix}1 & -i \\\\ i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 - 0.8i \\\\ 0.8 + 0.6i \\end{bmatrix} ;\\\\\n",
    "P_{-i} \\ket \\psi &= \\frac{1}{2} \\begin{bmatrix}1 & i \\\\ -i & 1\\end{bmatrix} \\begin{bmatrix} 0.6 \\\\ 0.8 \\end{bmatrix} = \\frac{1}{2} \\begin{bmatrix} 0.6 + 0.8i \\\\ 0.8 - 0.6i \\end{bmatrix}.\n",
    "\\end{align}\n",
    "Hence, the probabilities of measuring $\\pm i$, which we denote by $p(\\pm i)$, are\n",
    "\\begin{align}\n",
    "p(+i)& = |P_{+i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 - 0.8i|^2 + |0.8 + 0.6i|^2) = \\frac{1}{2}; \\\\\n",
    "p(-i)& = |P_{-i} \\ket \\psi|^2 = \\frac{1}{4}(|0.6 + 0.8i|^2 + |0.8 - 0.6i|^2) = \\frac{1}{2}.\n",
    "\\end{align}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 4 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-4:-The-outcome-probabilities-for-a-measurement-in-a-specified-basis)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 5</span>: Distinguishing orthogonal states - 1\n",
    "\n",
    "**Inputs:** A qubit which is guaranteed to be in either the $\\ket {\\psi_+}$ or the $\\ket{\\psi_-} $ state, where $\\ket {\\psi_+} = 0.6\\ket 0 + 0.8 \\ket 1 $ and $\\ket {\\psi_-} = 0.8\\ket 0 - 0.6 \\ket 1$.\n",
    "\n",
    "**Output:** `true` if the qubit was in the $\\ket {\\psi_+}$ state, or `false` if it was in the $\\ket{\\psi_-} $ state. The state of the qubit at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "We can distinguish between the states $\\ket{\\psi_\\pm}$ if we implement a measurement in the $\\{ \\ket{\\psi_+}, \\ket{\\psi_-}\\}$ basis. This can be done if we construct a unitary transformation which maps the $\\ket{\\psi_+}$ state to the $\\ket{0}$ state, and the $\\ket{\\psi_{-}}$ state to the $\\ket{1}$ state. \n",
    "\n",
    "We can notice that the $R_y$ rotation gate with $\\theta = 2 \\arctan \\frac{0.8}{0.6}$ is an appropriate transformation:\n",
    "\n",
    "\\begin{align}\n",
    "R_y(\\theta) \\ket 0 &= 0.6 \\ket 0 + 0.8 \\ket 1 = \\ket {\\psi_+} \\\\\n",
    "R_y(\\theta) \\ket 1 &= -0.8 \\ket 0 + 0.6 \\ket 1 = \\ket{\\psi_-}\n",
    "\\end{align}\n",
    "\n",
    "Thus, the inverse (adjoint) transformation $R_y(-\\theta)$ maps the $\\ket{\\psi_\\pm}$ basis to the computational basis, i.e.,\n",
    "\\begin{align}\n",
    "R_y(-\\theta) \\ket {\\psi_+} &= \\ket 0 \\\\\n",
    "R_y(-\\theta) \\ket {\\psi_-} &= \\ket 1\n",
    "\\end{align}\n",
    "\n",
    "Hence, if we apply $R_y(-\\theta)$ to the qubit, its state will be transformed to one of the computational basis states, at which point we can measure it using `M`. If `M` returns `Zero`, the rotated state is $\\ket{0}$, which means that the original state of the qubit was $\\ket{\\psi_+}$. Similarly, an output of `One` indicates that the qubit was originally in the state $\\ket{\\psi_-}$. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T5_IsQubitPsiPlus\n",
    "\n",
    "open Microsoft.Quantum.Math;\n",
    "\n",
    "operation IsQubitPsiPlus (q : Qubit) : Bool { \n",
    "    Ry(-2.0 * ArcTan2(0.8, 0.6), q);\n",
    "    return M(q) == Zero;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 5 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-5:-Distinguishing-orthogonal-states---1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 6</span>: Distinguishing orthogonal states - 2\n",
    "\n",
    "**Inputs:** \n",
    "\n",
    "1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
    "2. A qubit which is guaranteed to be in either the $|A\\rangle$ or the $|B\\rangle$ state, where $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$.\n",
    "\n",
    "**Output:** `true` if the qubit was in the $|A\\rangle$ state, or `false` if it was in the $|B\\rangle$ state. The state of the qubit at the end of the operation does not matter."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "We can distinguish between the states $\\ket{A}$ and $\\ket B$ if we implement a measurement in the $\\{ \\ket{A}, \\ket{B}\\}$ basis.  \n",
    "\n",
    "We can notice that the $R_x$ rotation gate with $\\theta = 2 \\alpha$ is an appropriate transformation which maps the $\\ket 0 $ state to the $\\ket A$ state, and the $\\ket 1$ state to the $\\ket B$ state:\n",
    "\n",
    "\\begin{align}\n",
    "R_x(\\theta) \\ket 0 &= \\cos \\alpha \\ket 0 -i \\sin \\alpha \\ket 1 = \\ket {A} \\\\\n",
    "R_x(\\theta) \\ket 1 &= -i \\sin \\alpha \\ket 0 + \\cos \\alpha \\ket 1 = \\ket{B}\n",
    "\\end{align}\n",
    "\n",
    "Thus, the inverse transformation $R_x(-\\theta)$ maps the $A/B$ basis to the $0/1$ basis.\n",
    "\n",
    "Therefore, if we apply $R_x(-\\theta)$ to the qubit and measure it using `M`, a measurement result of `Zero` will correspond to the qubit's original state being $\\ket{A}$, while a result of `One` will correspond to the qubit's original state being $\\ket B$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "%kata T6_IsQubitA\n",
    "\n",
    "operation IsQubitA (alpha: Double, q : Qubit) : Bool { \n",
    "    Rx(-2.0 * alpha, q);\n",
    "    return M(q) == Zero;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 6 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-6:-Distinguishing-orthogonal-states---2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <span style=\"color:blue\">Exercise 7</span>: Measurement in the $\\ket A$, $\\ket B$ basis\n",
    "\n",
    "**Inputs:** \n",
    "1. Angle $\\alpha$, in radians, represented as a `Double`.\n",
    "1. A qubit in some unknown state.\n",
    "\n",
    "\n",
    "**Output:** Implement a measurement in the $\\{\\ket A$, $\\ket B\\}$ basis. Same as in the previous exercise, $|A\\rangle = \\cos \\alpha |0\\rangle - i \\sin \\alpha |1\\rangle$ and $|B\\rangle = - i \\sin \\alpha |0\\rangle + \\cos \\alpha |1\\rangle$. Return `Zero` if the measurement outcome is $A$, and `One` if the outcome is $B$. \n",
    "The state of the qubit after the measurement should correspond to the measurement result."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Solution\n",
    "For this problem, we follow the procedure given in the [arbitrary basis measurement section](./SingleQubitSystemMeasurements.ipynb#Arbitrary-basis-measurements-implementation) of the tutorial.\n",
    "As noted in the solution of problem 6, the gate $R_x(-\\theta)$, with $\\theta = 2\\alpha$ transforms the $\\ket A/\\ket B$ states to the $\\ket 0/\\ket 1$ states:\n",
    "\\begin{align}\n",
    "R_x(-\\theta) \\ket A &= \\ket 0 \\\\\n",
    "R_X(-\\theta) \\ket B &= \\ket 1\n",
    "\\end{align}\n",
    "Hence, we first apply $R_x(-\\theta)$ to the qubit. Next, we measure in the computational basis using the `M` operation. \n",
    "If the `M` operation returned `Zero`, we get measurement outcome $A$, and if it returned `One`, we get measurement outcome $B$. \n",
    "\n",
    "After the measurement, we apply the inverse of the $R_x(-\\theta)$ gate, which is the $R_x(\\theta)$ gate.\n",
    "The final rotation ensures that the state of the qubit is in the state corresponding to the measurement outcome."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "%kata T7_MeasureInABBasis\n",
    "\n",
    "operation MeasureInABBasis (alpha: Double, q : Qubit) : Result {\n",
    "    Rx(-2.0 * alpha, q);\n",
    "    let measurementResult = M(q);\n",
    "    Rx(2.0 * alpha, q);\n",
    "    return measurementResult;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "[Return to exercise 7 of the Single-Qubit System Measurements tutorial.](./SingleQubitSystemMeasurements.ipynb#Exercise-7:-Measurement-in-the-$\\ket-A$,-$\\ket-B$-basis)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Q#",
   "language": "qsharp",
   "name": "iqsharp"
  },
  "language_info": {
   "file_extension": ".qs",
   "mimetype": "text/x-qsharp",
   "name": "qsharp",
   "version": "0.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
